<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-button class="filter-item" style="margin-left: 10px;" @click="handleCreate" type="primary" icon="el-icon-edit">{{$t('table.add')}}</el-button>
    </div>
    <el-table :key='tableKey' :data="list" v-loading="listLoading" element-loading-text="给我一点时间" border fit >

      <el-table-column min-width="100px" align="center" :label="$t('video.title')">
        <template slot-scope="scope">
          <span>{{ scope.row.title }}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="100px" align="center" :label="$t('video.type')">
        <template slot-scope="scope">
          <span>{{ scope.row.type === 1 ? '考级视频' : '教师认证视频' }}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="100px" align="center" :label="$t('video.url')">
        <template slot-scope="scope">
          <span>{{ scope.row.url }}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="100px" align="center" :label="$t('video.price')">
        <template slot-scope="scope">
          <span>{{ scope.row.price / 100 }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" :label="$t('table.actions')" min-width="100px" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">{{$t('table.edit')}}</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">{{$t('table.delete')}}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page"
        :page-sizes="[10,20,30, 50]" :page-size="listQuery.size" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" width="35%" style='margin-top:-3%;'>
      <el-form :rules="rules" ref="dataForm" :model="temp" label-position="left" label-width="120px" style='margin-left:20px;'>
        <el-form-item :label="$t('video.title')" prop="title">
          <el-input v-model="temp.title"></el-input>
        </el-form-item>
        <el-form-item :label="$t('video.type')" prop="type">
        <el-select  v-model="temp.type"  placeholder="分类">
          <el-option
              v-for="item in types"
              :key="item.key"
              :label="item.value"
              :value="item.key">
          </el-option>
      </el-select>
        </el-form-item>
        <el-form-item :label="$t('video.url')" prop="url">
          <el-input v-model="temp.url"></el-input>
        </el-form-item>
        <el-form-item :label="$t('video.price')" prop="price">
          <el-input v-model="temp.price"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">{{$t('table.cancel')}}</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="createData">{{$t('table.confirm')}}</el-button>
        <el-button v-else type="primary" @click="updateData">{{$t('table.confirm')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { videos, create, remove, update } from '@/api/video'

export default {
  name: 'services',
  data() {
    return {
      tableKey: 0,
      list: [],
      types: [{
        key: 1,
        value: '考级视频'
      }, {
        key: 2,
        value: '教师认证视频'
      }],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        size: 10
      },
      temp: {
        id: undefined,
        title: undefined,
        url: undefined,
        price: undefined,
        type: undefined
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增'
      },
      rules: {
        title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        type: [{ required: true, message: '请输入分类', trigger: 'blur' }],
        url: [{ required: true, message: '链接', trigger: 'blur' }],
        price: [{ required: true, message: '价格', trigger: 'blur' }]
      }
    }
  },
  filters: {
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      videos(this.listQuery).then(response => {
        this.list = response.data.data
        this.listQuery.page = response.data.page.pageNum === 0 ? 1 : response.data.page.pageNum
        this.listQuery.size = response.data.page.pageSize
        this.total = response.data.page.total
        this.listLoading = false
      })
    },
    handleSizeChange(val) {
      this.listQuery.size = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        title: undefined,
        url: undefined,
        price: undefined,
        type: undefined
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        this.temp.price = this.temp.price * 100
        if (valid) {
          create(this.temp).then(rep => {
            this.temp.id = rep.data.data
            this.list.unshift(this.temp)
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.temp.price = this.temp.price / 100
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          tempData.price = tempData.price * 100
          update(tempData).then(() => {
            for (const v of this.list) {
              if (v.id === this.temp.id) {
                const index = this.list.indexOf(v)
                this.temp.price = this.temp.price * 100
                this.list.splice(index, 1, this.temp)
                break
              }
            }
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleDelete(row) {
      remove(row.id).then((res) => {
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 2000
        })
        const index = this.list.indexOf(row)
        this.list.splice(index, 1)
        this.total--
      })
    }
  }
}
</script>
